package com.whh.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.whh.config.exception.APIException;
import com.whh.config.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;

import com.whh.dao.BaseDao;
import com.whh.dao.MassageDao;
import com.whh.entity.BaseEntity;
import com.whh.entity.ResultEntity;
import com.whh.entity.SysMassage;
import com.whh.service.BaseService;

public class BaseServiceImpl<T extends BaseEntity> implements BaseService<T> {

	
	private BaseDao<T> dao;
	
	public BaseDao<T> getDao() {
		return dao;
	}

	public void setDao(BaseDao<T> dao) {
		this.dao = dao;
	}
	
	protected String loginUserName;
	
	protected int loginUserId;
	
	@Autowired
	private MassageDao message;
	
	@Override
	public T add(T entity) {
		if(entity==null) {
			throw new APIException(ResultCode.ERROR,"新增接口参数为空");
		}
		if(entity.getState()==null){
			entity.setState(0);
		}
		doBeforeAdd(entity);
		doAdd(entity);
		entity.setObjType(getObjType(entity));
		doAfterAdd(entity);
		return entity;
	}

	protected void doAfterAdd(T entity) {
		SysMassage msg = new SysMassage();
		msg.setAction("创建了");
		msg.setOperator(entity.getLoginUserName());
		msg.setOperatorId(entity.getLoginUserId());
		msg.setObject(entity.getObjType());
		msg.setObjectId(entity.getId());
		msg.setObjectName(entity.getName());
		if(!"系统".equals(entity.getObjType())) {
			msg.setBelongProduct(entity.getBelongProduct());
			msg.setBelongProject(entity.getBelongProject());
			msg.setBelongTask(entity.getBelongTask());
			msg.setBelongBug(entity.getBelongBug());
			msg.setBelongDemand(entity.getBelongDemand());
			if("产品".equals(entity.getObjType())) {
				msg.setBelongProduct(entity.getId());
			}else if("项目".equals(entity.getObjType())){
				msg.setBelongProject(entity.getId());
			}
		}
		message.add(msg);
	}

	protected void doAdd(T entity) {
		if (entity.getName() != null) {
			Integer n = dao.add(entity);
			if(n==null|| n ==0) {
				throw new APIException(ResultCode.ERROR,"新增至数据库失败");
			}
		}else {
			throw new APIException(ResultCode.ERROR,"新增失败,name字段不能为空");
		}
		
	}
	protected void doBeforeAdd(T entity) {
		if(entity.getLoginUserId() == 0 || entity.getLoginUserName() == null) {
			throw new APIException(ResultCode.ERROR,"请添加操作人的id和姓名");
		}
	}

	@Override
	public Integer del(T entity) {
		ResultEntity result = new ResultEntity();
		if(entity.getId() == 0) {
			result.setMsg("参数为空");
			throw new APIException(ResultCode.VALIDATE_FAILED,"参数为空");
		}
		if(dao.getById(entity.getId())==null) {
			throw new APIException(ResultCode.VALIDATE_FAILED,"id不存在");
		}
		doBeforeDel(entity);
		entity = dao.getById(entity.getId());
		Integer n = doDel(entity);
		doAfterDel(entity);
		return n;
	}

	protected void doAfterDel(T entity) {
		SysMassage msg = new SysMassage();
		msg.setAction("删除了");
		msg.setOperator(loginUserName);
		msg.setOperatorId(loginUserId);
		msg.setObject(entity.getObjType());
		msg.setObjectId(entity.getId());
		msg.setObjectName(entity.getName());
		if(!"系统".equals(entity.getObjType())) {
			msg.setBelongProduct(entity.getBelongProduct());
			msg.setBelongProject(entity.getBelongProject());
			msg.setBelongTask(entity.getBelongTask());
			msg.setBelongBug(entity.getBelongBug());
			msg.setBelongDemand(entity.getBelongDemand());
			if("产品".equals(entity.getObjType())) {
				msg.setBelongProduct(entity.getId());
			}else if("项目".equals(entity.getObjType())){
				msg.setBelongProject(entity.getId());
			}
		}
		message.add(msg);
	}

	protected void doBeforeDel(T entity) {
		loginUserName = entity.getLoginUserName();
		loginUserId = entity.getLoginUserId();
		if(loginUserId == 0 || loginUserName == null) {
			throw new APIException(ResultCode.ERROR,"请添加操作人的id和姓名");
		}
	}
	
	protected Integer doDel(T entity) {
		return dao.del(entity);
	}
	
	protected String getObjType(T entity) {
		return dao.getObjTypeById(entity);
	}
	
	@Override
	public T update(T entity) {
		if(entity==null) {
			throw new APIException(ResultCode.VALIDATE_FAILED,"参数为空");
		}
		T t = dao.getById(entity.getId());
		if(t==null) {
			throw new APIException(ResultCode.VALIDATE_FAILED,"id不存在");
		}
		doBeforeUpdate(entity);
		Integer n = dao.update(entity);
		if(n!=null&& n >0) {
			entity = dao.getById(entity.getId());
		}
		doAfterUpdate(t);
		return t;
	}

	protected void doAfterUpdate(T entity) {
		SysMassage msg = new SysMassage();
		msg.setAction("修改了");
		msg.setOperator(loginUserName);
		msg.setOperatorId(loginUserId);
		msg.setObject(entity.getObjType());
		msg.setObjectId(entity.getId());
		msg.setObjectName(entity.getName());
		if(!"系统".equals(entity.getObjType())) {
			msg.setBelongProduct(entity.getBelongProduct());
			msg.setBelongProject(entity.getBelongProject());
			msg.setBelongTask(entity.getBelongTask());
			msg.setBelongBug(entity.getBelongBug());
			msg.setBelongDemand(entity.getBelongDemand());
			if("产品".equals(entity.getObjType())) {
				msg.setBelongProduct(entity.getId());
			}else if("项目".equals(entity.getObjType())){
				msg.setBelongProject(entity.getId());
			}
		}
		message.add(msg);
	}

	protected void doBeforeUpdate(T source) {
		loginUserName=source.getLoginUserName();
		loginUserId=source.getLoginUserId();
		if(loginUserId == 0 || loginUserName == null) {
			throw new APIException(ResultCode.VALIDATE_FAILED,"请添加操作人的id和姓名");
		}
	}

	@Override
	public List<T> search(T entity) {
		return dao.search(entity);
	}

	@Override
	public T getById(Integer id) {
		if(id==null) {
			throw new APIException(ResultCode.VALIDATE_FAILED,"参数为空");
		}
		return dao.getById(id);
	}
	@Override
	public List<T> sum(T entity) {
		return dao.sum(entity);
	}

	@Override
	public List<T> total(T entity) {
		return dao.total(entity);
	}
	
	

	@Override
	public T init(T entity) {
		doInit(entity);
		return entity;
	}
	
	protected void doInit(T entity) {
		
	}

	@Override
	public ResultEntity searchByPage(T entity, Integer currPage, Integer pageSize) {
		ResultEntity result = new ResultEntity();
		Long records = dao.getRecordsForPage(entity);
		result.setPage(currPage);
		result.setRows(pageSize);
		result.setRecords(records);
		
		if(records!=null&&records.longValue()>0) {
			List<T> data = dao.searchByPage(entity,currPage,pageSize);
			doAfterSearch(data,result);
			result.setData(data);
		}else {
			doAfterSearch(new ArrayList<T>(),result);
		}
		
		return result;
	}

	protected void doAfterSearch(List<T> data, ResultEntity result) {
		// TODO Auto-generated method stub
		
	}
}
